package code.class3.easy;

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/minimum-average-of-smallest-and-largest-elements/description/
 * 最小元素和最大元素的最小平均值
 */
public class Code03_minimumAverage {
    public static void main(String[] args) {
        int[] arr = {1, 9, 8, 3, 10, 5};
        double res = minimumAverage(arr);
        System.out.println("res = " + res);
    }

    public static double minimumAverage(int[] arr) {
        Arrays.sort(arr);
        int p1 = 0, p2 = arr.length - 1, ans = Integer.MAX_VALUE;
        while (p1 < p2) {
            ans = Math.min((arr[p1++] + arr[p2--]), ans);
        }
        // 返回答案 时才除以 2！既能避免浮点数比较问题，又能节省时间
        return ans / 2.0;
    }
}
